<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain :icon="Plus" @click="handleAdd"
                    v-hasPermi="['manager:operationTrain:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain :icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['manager:operationTrain:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain :icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['manager:operationTrain:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain :icon="Download" @click="handleExport"
                    v-hasPermi="['manager:operationTrain:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="operationTrainList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="主键" align="center" prop="trainId" v-if="true" />
            <el-table-column label="标题" align="center" prop="title">
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right">
                <template #default="scope">
                    <el-button link style="color:#265DA4;" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['manager:operationTrain:edit']">修改</el-button>
                    <el-button link style="color:#265DA4;" icon="Edit" @click="handleDelete(scope.row)"
                        v-hasPermi="['manager:operationTrain:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" /> -->

        <!-- 添加或修改实操训练试卷对话框 -->
        <el-dialog v-model="open" :title="title" width="60%" append-to-body>
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="标题" prop="title">
                            <el-input v-model="form.title" placeholder="请输入标题" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="报警电话" prop="callPhone">
                            <el-input v-model="form.callPhone" placeholder="请输入报警电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="报警人员" prop="callPerson">
                            <el-input v-model="form.callPerson" placeholder="请输入报警人员" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="报警地址" prop="callAddress">
                            <el-input v-model="form.callAddress" placeholder="请输入报警地址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="标签" prop="tag">
                            <el-select v-model="form.tag" popper-class="exam-selct__popper" placeholder="请选择标签">
                                <el-option v-for="item in train_tag" :key="item.value" :label="item.label"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="是否是被困人员" prop="isBesiege">
                            <el-checkbox v-model="form.isBesiege" :true-label="1" :false-label="0" label="被困人员" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系电话" prop="phone">
                            <el-input v-model="form.phone" placeholder="请输入联系电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="重点单位" prop="keyUnit">
                            <el-input v-model="form.keyUnit" placeholder="请输入重点单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="案发地址" prop="happenAddress">
                            <el-input v-model="form.happenAddress" placeholder="请输入案发地址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所属区县" prop="area">
                            <el-cascader v-model="form.area" :options="areaOptions" :props="areaProps"
                                @change="handleAreaChange" placeholder="请选择所属区县" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="主管机构" prop="org">
                            <el-input v-model="form.org" placeholder="请输入主管机构" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="作战信息" prop="fightInfo">
                            <el-input v-model="form.fightInfo" placeholder="请输入作战信息" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="案件标签" prop="caseTag">
                            <el-input v-model="form.caseTag" placeholder="请输入案件标签" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="案件描述" prop="caseDescribe">
                            <el-input v-model="form.caseDescribe" placeholder="请输入案件描述" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="火灾等级" prop="fireLevel">
                            <el-input v-model="form.fireLevel" placeholder="请输入火灾等级" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12"> <el-form-item label="燃烧对象" prop="burnObject">
                            <el-select v-model="form.burnObject" popper-class="exam-selct__popper" placeholder="建筑堆场类">
                                <el-option v-for="item in train_burn_object" :key="item.value" :label="item.label"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item></el-col>
                    <el-col :span="12"> <el-form-item label="烟雾状况" prop="smokeCondition">
                            <el-select v-model="form.smokeCondition" popper-class="exam-selct__popper"
                                placeholder="请选择烟雾状况">
                                <el-option v-for="item in train_smoke_condition" :key="item.value" :label="item.label"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item></el-col>
                    <el-col :span="12"> <el-form-item label="建筑结构" prop="buildingStructure">
                            <el-select v-model="form.buildingStructure" popper-class="exam-selct__popper"
                                placeholder="请选择建筑结构">
                                <el-option v-for="item in train_building_structure" :key="item.value" :label="item.label"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item></el-col>
                    <el-col :span="12"> <el-form-item label="楼房层数" prop="buildingFloors">
                            <el-input v-model="form.buildingFloors" placeholder="请输入楼房层数" />
                        </el-form-item></el-col>
                    <el-col :span="12"> <el-form-item label="燃烧层数" prop="burnFloors">
                            <el-input v-model="form.burnFloors" placeholder="请输入燃烧层数" />
                        </el-form-item></el-col>
                    <el-col :span="12"> <el-form-item label="燃烧面积" prop="burnArea">
                            <el-input v-model="form.burnArea" placeholder="请输入燃烧面积" />
                        </el-form-item></el-col>
                    <el-col :span="12"><el-form-item label="被困人数" prop="trappedNum">
                            <el-input v-model="form.trappedNum" placeholder="请输入被困人数" />
                        </el-form-item></el-col>
                    <el-col :span="12"> <el-form-item label="受伤人数" prop="injuryNum">
                            <el-input v-model="form.injuryNum" placeholder="请输入受伤人数" />
                        </el-form-item></el-col>
                    <el-col :span="12"> <el-form-item label="死亡人数" prop="deathNum">
                            <el-input v-model="form.deathNum" placeholder="请输入死亡人数" />
                        </el-form-item></el-col>
                    <el-col :span="12">
                        <el-form-item label="警情提示" prop="warnTip">
                            <el-input v-model="form.warnTip" placeholder="请输入警情提示" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24"><el-form-item label="救援力量" prop="rescueForces">
                            <template #default>
                                <!-- <el-input v-model="form.rescueForces" placeholder="请输入救援力量" /> -->
                                <el-select v-model="rescueForcesData.deptId" filterable popper-class="exam-selct__popper"
                                    placeholder="请选择关键字" @change="selectDeptChange">
                                    <el-option v-for="item in stationList" :key="item.deptId" :label="item.deptName"
                                        :value="item.deptId" :disabled="!item.deptCarNum">
                                        <span style="float: left">{{ item.deptName }}</span>
                                        <span
                                            style="float: right;color: var(--el-text-color-secondary); font-size: 13px;">{{
                                                item.deptCarNum }}</span>
                                    </el-option>
                                </el-select>
                                <el-select v-model="rescueForcesData.carIdList" filterable multiple
                                    popper-class="exam-selct__popper" placeholder="请选择关键字">
                                    <el-option v-for="item in fireCardList" :key="item.id" :label="item.carName"
                                        :value="parseInt(item.id)" />
                                </el-select>
                            </template>
                        </el-form-item></el-col>
                    <el-col :span="24"> <el-form-item label="简单录音模板" prop="simpleTemplate">
                            <template #default>
                                <el-table :data="templateData.simple" style="width: 100%" max-height="250">
                                    <el-table-column fixed label="关键字" prop="keyword" width="150">
                                        <template #default="{ row }">
                                            <el-select v-model="row.keyword" popper-class="exam-selct__popper"
                                                placeholder="请选择关键字">
                                                <el-option v-for="item in keywordDict" :key="item.columnName"
                                                    :label="item.columnComment" :value="item.columnName" />
                                            </el-select>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="keywordLabel" label="输入关键字答案">
                                        <template #default="{ row }">
                                            <el-input v-model="row.keywordLabel" placeholder="请输入关键字答案" />
                                        </template>
                                    </el-table-column>
                                    <el-table-column fixed="right" label="操作" width="120">
                                        <template #default="{ $index }">
                                            <el-button link type="primary" size="small"
                                                @click.prevent="deleteTemplateRow($index, 'simple')">
                                                删除
                                            </el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                                <el-button class="mt-4" style="width: 100%"
                                    @click="onAddTemplateItem('simple')">添加关键字</el-button>
                            </template>
                        </el-form-item></el-col>
                    <el-col :span="24">
                        <el-form-item label="完整录音模板" prop="fullTemplate">
                            <template #default="scope">
                                <!-- <el-input v-model="form.fullTemplate" placeholder="请输入完整录音模板" /> -->
                                <el-table :data="templateData.full" style="width: 100%" max-height="250">
                                    <el-table-column fixed label="关键字" prop="keyword" width="150">
                                        <template #default="{ row }">
                                            <el-select v-model="row.keyword" popper-class="exam-selct__popper"
                                                placeholder="请选择关键字">
                                                <el-option v-for="item in keywordDict" :key="item.columnName"
                                                    :label="item.columnComment" :value="item.columnName" />
                                            </el-select>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="keywordLabel" label="输入关键字答案">
                                        <template #default="{ row }">
                                            <el-input v-model="row.keywordLabel" placeholder="请输入关键字答案" />
                                        </template>
                                    </el-table-column>
                                    <el-table-column fixed="right" label="操作" width="120">
                                        <template #default="{ $index }">
                                            <el-button link type="primary" size="small"
                                                @click.prevent="deleteTemplateRow($index, 'full')">
                                                删除
                                            </el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                                <el-button class="mt-4" style="width: 100%"
                                    @click="onAddTemplateItem('full')">添加关键字</el-button>
                            </template>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24"><el-form-item label="询问关键字" prop="inquiryKeywords">
                            <template #default="{ row }">
                                <!-- <el-input v-model="form.inquiryKeywords" placeholder="请输入询问关键字" /> -->
                                <el-table :data="inquiryKeyWordData" style="width: 100%" max-height="250">
                                    <el-table-column fixed label="关键字" prop="field" width="150">
                                        <template #default="{ row }">
                                            <el-select v-model="row.field" popper-class="exam-selct__popper"
                                                placeholder="请选择关键字">
                                                <el-option v-for="item in keywordDict" :key="item.columnName"
                                                    :label="item.columnComment" :value="item.columnName" />
                                            </el-select>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="keywords" label="输入关键字答案">
                                        <template #default="{ row }">
                                            <!-- <el-input v-model="row.keywords" placeholder="请输入关键字答案" /> -->
                                            <el-tag v-for="tag in row.keywords" :key="tag" class="mx-1" closable
                                                :disable-transitions="false" @close="handleTagClose(tag, row)">
                                                {{ tag }}
                                            </el-tag>
                                            <el-input v-if="row.tagInputVisible" ref="InputRef" v-model="row.keywordInput"
                                                class="ml-1 w-20" size="small" @keyup.enter="handleInputConfirm(row)"
                                                @blur="handleInputConfirm(row)" />
                                            <el-button v-else class="button-new-tag ml-1" size="small"
                                                @click="showInput(row)">
                                                + 添加关键字
                                            </el-button>
                                        </template>
                                    </el-table-column>
                                    <el-table-column fixed="right" label="操作" width="120">
                                        <template #default="{ $index }">
                                            <el-button link type="primary" size="small"
                                                @click.prevent="deleteInquiryKeyRow($index)">
                                                删除
                                            </el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                                <el-button class="mt-4" style="width: 100%"
                                    @click="onAddInquiryKeyWordItem">添加关键字</el-button>
                            </template>
                        </el-form-item></el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script setup>
import { Delete, Edit, Download, Plus } from '@element-plus/icons-vue'
import { listOperationTrain, getOperationTrain, delOperationTrain, addOperationTrain, updateOperationTrain, getTableColumnList, getOperationTrainCarList, getOperationTrainDeptList } from "@/api/manage/operationTrain";
import { getSysArea } from '@/api/system/sysArea';
import { ElMessage, ElMessageBox } from "element-plus";
const { proxy } = getCurrentInstance();
const { train_tag, train_burn_object, train_smoke_condition, train_building_structure } = proxy.useDict("train_tag", "train_burn_object", "train_smoke_condition", "train_building_structure");
// 按钮loading
const buttonLoading = ref(false)
// 遮罩层
const loading = ref(true)
// 选中数组
const ids = ref([])
// 非单个禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(false)
// 总条数
const total = ref(0)
// 实操训练试卷表格数据
const operationTrainList = ref([])
// 弹出层标题
const title = ref("")
// 是否显示弹出层
const open = ref(false)
// 查询参数
const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    callPhone: undefined,
    callPerson: undefined,
    callAddress: undefined,
    tag: undefined,
    isBesiege: undefined,
    phone: undefined,
    keyUnit: undefined,
    happenAddress: undefined,
    area: undefined,
    org: undefined,
    fightInfo: undefined,
    caseTag: undefined,
    caseDescribe: undefined,
    caseType: undefined,
    fireLevel: undefined,
    burnObject: undefined,
    smokeCondition: undefined,
    buildingStructure: undefined,
    buildingFloors: undefined,
    burnFloors: undefined,
    burnArea: undefined,
    trappedNum: undefined,
    injuryNum: undefined,
    deathNum: undefined,
    warnTip: undefined,
    rescueForces: undefined,
    simpleTemplate: undefined,
    fullTemplate: undefined,
    trainStatus: undefined,
    inquiryKeywords: undefined,
    status: undefined,
})
// 表单参数
const form = ref({})
// 表单校验
const rules = ref({
    title: [
        { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    trainId: [
        { required: true, message: "主键不能为空", trigger: "blur" }
    ],
    callPhone: [
        { required: true, message: "报警电话不能为空", trigger: "blur" }
    ],
    callPerson: [
        { required: true, message: "报警人员不能为空", trigger: "blur" }
    ],
    callAddress: [
        { required: true, message: "报警地址不能为空", trigger: "blur" }
    ],
    tag: [
        { required: true, message: "标签不能为空", trigger: "blur" }
    ],
    isBesiege: [
        { required: true, message: "是否是被困人员不能为空", trigger: "blur" }
    ],
    phone: [
        { required: true, message: "联系电话不能为空", trigger: "blur" }
    ],
    keyUnit: [
        { required: true, message: "重点单位不能为空", trigger: "blur" }
    ],
    happenAddress: [
        { required: true, message: "案发地址不能为空", trigger: "blur" }
    ],
    area: [
        { required: true, message: "所属区县不能为空", trigger: "blur" }
    ],
    org: [
        { required: true, message: "主管机构不能为空", trigger: "blur" }
    ],
    fightInfo: [
        { required: true, message: "作战信息不能为空", trigger: "blur" }
    ],
    caseTag: [
        { required: true, message: "案件标签不能为空", trigger: "blur" }
    ],
    caseDescribe: [
        { required: true, message: "案件描述不能为空", trigger: "blur" }
    ],
    caseType: [
        { required: true, message: "案件类型不能为空", trigger: "change" }
    ],
    fireLevel: [
        { required: true, message: "火灾等级不能为空", trigger: "blur" }
    ],
    burnObject: [
        { required: true, message: "燃烧对象不能为空", trigger: "blur" }
    ],
    smokeCondition: [
        { required: true, message: "烟雾状况不能为空", trigger: "blur" }
    ],
    buildingStructure: [
        { required: true, message: "建筑结构不能为空", trigger: "blur" }
    ],
    buildingFloors: [
        { required: true, message: "楼房层数不能为空", trigger: "blur" }
    ],
    burnFloors: [
        { required: true, message: "燃烧层数不能为空", trigger: "blur" }
    ],
    burnArea: [
        { required: true, message: "燃烧面积不能为空", trigger: "blur" }
    ],
    trappedNum: [
        { required: true, message: "被困人数不能为空", trigger: "blur" }
    ],
    injuryNum: [
        { required: true, message: "受伤人数不能为空", trigger: "blur" }
    ],
    deathNum: [
        { required: true, message: "死亡人数不能为空", trigger: "blur" }
    ],
    warnTip: [
        { required: true, message: "警情提示不能为空", trigger: "blur" }
    ],
    rescueForces: [
        { required: true, message: "救援力量不能为空", trigger: "blur" }
    ],
    // simpleTemplate: [
    //     { required: true, message: "简单录音模板不能为空", trigger: "blur" }
    // ],
    fullTemplate: [
        { required: true, message: "完整录音模板不能为空", trigger: "blur" }
    ],
    trainStatus: [
        { required: true, message: "实操试卷状态(0:启用，1:归档(未启用))不能为空", trigger: "change" }
    ],
    inquiryKeywords: [
        { required: true, message: "询问关键字不能为空", trigger: "blur" }
    ],
    status: [
        { required: true, message: "状态(0：正常，1:删除)不能为空", trigger: "change" }
    ],
})
getList();
/** 查询实操训练试卷列表 */
function getList() {
    loading.value = true;
    listOperationTrain(queryParams.value).then(response => {
        operationTrainList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}
// 取消按钮
function cancel() {
    open.value = false;
    reset();
}
// 表单重置
function reset() {
    form.value = {
        trainId: undefined,
        callPhone: undefined,
        callPerson: undefined,
        callAddress: undefined,
        tag: undefined,
        isBesiege: undefined,
        phone: undefined,
        keyUnit: undefined,
        happenAddress: undefined,
        area: undefined,
        org: undefined,
        fightInfo: undefined,
        caseTag: undefined,
        caseDescribe: undefined,
        caseType: undefined,
        fireLevel: undefined,
        burnObject: undefined,
        smokeCondition: undefined,
        buildingStructure: undefined,
        buildingFloors: undefined,
        burnFloors: undefined,
        burnArea: undefined,
        trappedNum: undefined,
        injuryNum: undefined,
        deathNum: undefined,
        warnTip: undefined,
        rescueForces: undefined,
        simpleTemplate: undefined,
        fullTemplate: undefined,
        trainStatus: undefined,
        inquiryKeywords: undefined,
        status: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
    };
    proxy.resetForm("formRef");
}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryForm");
    handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.trainId)
    single.value = selection.length !== 1
    multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    form.value = {}
    rescueForcesData.deptId = []
    rescueForcesData.carIdList = []
    // 简单/完整录音
    templateData.simple = []
    templateData.full = []
    inquiryKeyWordData.value = []
    title.value = "添加实操训练试卷";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    loading.value = true;
    reset();
    const trainId = row.trainId || ids.value
    getOperationTrain(trainId).then(response => {
        loading.value = false;
        form.value = response.data;
        templateConvertArray(form.value.simpleTemplate, "simple")
        templateConvertArray(form.value.fullTemplate, 'full')
        inquiryKeywordConvertArray()
        rescueForcesConvert()
        open.value = true;
        title.value = "修改实操训练试卷";
    });
}
/** 提交按钮 */
function submitForm() {

    proxy.$refs["formRef"].validate(valid => {
        ``
        if (valid) {
            buttonLoading.value = true;
            if (form.value.trainId != null) {
                form.value.rescueForces = JSON.stringify(rescueForcesData)
                let fullStr = ''
                templateData.full.forEach(item => {
                    if (item.keyword) {
                        fullStr += `[${item.keyword}]${item.keywordLabel}`
                    }
                })

                let simpleStr = ''
                templateData.simple.forEach(item => {
                    if (item.keyword) {
                        simpleStr += `[${item.keyword}]${item.keywordLabel}`
                    }
                })
                form.value.simpleTemplate = simpleStr
                form.value.fullTemplate = fullStr
                const inquiryKeyWordArr = inquiryKeyWordData.value.map(item => {
                    return {
                        field: item.field,
                        keywords: item.keywords
                    }
                })
                form.value.inquiryKeywords = JSON.stringify(inquiryKeyWordArr)
                updateOperationTrain(form.value).then(response => {
                    ElMessage({
                        message: '修改成功',
                        type: 'success',
                    })
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            } else {
                addOperationTrain(form.value).then(response => {
                    ElMessage({
                        message: '新增成功',
                        type: 'success',
                    })
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            }
        }
    });
}
/** 删除按钮操作 */
function handleDelete(row) {
    const trainIds = row.trainId || ids.value;
    loading.value = true;
    delOperationTrain(trainIds);
    ElMessageBox.confirm(
        '是否确认删除实操训练试卷编号为"' + trainIds + '"的数据项？',
        'Warning',
        {
            confirmButtonText: 'OK',
            cancelButtonText: 'Cancel',
            type: 'warning',
        }
    ).then(() => {
        loading.value = false;
        getList();
        ElMessage({
            type: 'success',
            message: '删除成功',
        })
    })
        .catch(() => {
            loading.value = false;
        })
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('manager/operationTrain/export', {
        ...queryParams.value
    }, `operationTrain_${new Date().getTime()}.xlsx`)
}

/* 模板相关 */
const templateData = reactive({
    simple: [],
    full: [],
})

const onAddTemplateItem = (key) => {
    let arr = templateData[key].concat([{ keyword: '', keywordLabel: '' }])
    templateData[key] = arr
}

function deleteTemplateRow(index, key) {
    templateData[key].splice(index, 1)
}

const keywordDict = ref([])
function getFullTemplateKeyword() {
    getTableColumnList().then(res => {
        console.log(res);
        keywordDict.value = res.data
    })
}
getFullTemplateKeyword()

function templateConvertArray(fullTemplate, wordkey) {
    const inputString = fullTemplate
    // 使用正则表达式匹配
    const matches = inputString.match(/\[([^[\]]*)\]/g);

    // 将匹配到的内容转换为对象
    const resultArray = [];
    if (matches) {
        matches.forEach(match => {
            const key = match.slice(1, -1); // 去掉方括号
            const value = inputString.split(match)[1].split((matches[matches.indexOf(match) + 1] || matches[0]))[0].trim(); // 获取方括号后的文本内容
            resultArray.push({
                keyword: key,
                keywordLabel: value
            })
        });
    }
    templateData[wordkey] = resultArray
    return resultArray
}

// 询问关键词
const inquiryKeyWordData = ref([])

function inquiryKeywordConvertArray() {
    const inquiryJsonArr = JSON.parse(form.value.inquiryKeywords)
    inquiryKeyWordData.value = inquiryJsonArr.map(item => {
        return {
            ...item,
            tagInputVisible: false,
            keywordInput: ''
        }
    })
}

const onAddInquiryKeyWordItem = () => {
    let arr = inquiryKeyWordData.value.concat([{ keywords: [], field: '' }])
    inquiryKeyWordData.value = arr
}

const InputRef = ref(null)
function handleTagClose(tag, row) {
    row.keywords.splice(row.keywords.indexOf(tag), 1)
}
function handleInputConfirm(row) {
    if (row.keywordInput) {
        row.keywords.push(row.keywordInput)
    }
    row.tagInputVisible = false
    row.keywordInput = ''
}
function showInput(row) {
    row.tagInputVisible = true
    nextTick(() => {
        InputRef.value.input.focus()
    })
}

function deleteInquiryKeyRow(index) {
    inquiryKeyWordData.value.splice(index, 1)
}

/* 救援力量相关 */
const rescueForcesData = reactive({
    deptId: '',
    carIdList: []
})
function rescueForcesConvert() {
    const rescueForces = JSON.parse(form.value.rescueForces)
    rescueForcesData.deptId = rescueForces.deptId
    rescueForcesData.carIdList = rescueForces.carIdList
}

function selectDeptChange(e) {
    console.log(e + '');
    getFireCar(e)
}

const stationList = ref([]);
function getStationList(params = { deptName: '' }) {
    getOperationTrainDeptList(params).then(res => {
        stationList.value = res.data
        getFireCar(res.data[0].deptId)
    })
}
getStationList()

const fireCardList = ref([])
function getFireCar(deptId) {
    getOperationTrainCarList({ deptId: deptId + '' }).then(res => {
        fireCardList.value = res.data
    })
}
/* 所属区县 */
const areaProps = reactive({
    expandTrigger: 'hover',
    label: 'shortName',
    value: 'id',
    emitPath: false
})
const areaOptions = ref([])

function getArea() {
    getSysArea().then(res => {
        areaOptions.value = res.data
    })
}
getArea()
/* 所属区县 */
function handleAreaChange(e) {
    console.log(e);
}
</script>
